package cn.fayang.org.controller;

import cn.fayang.basic.util.AjaxResult;
import cn.fayang.basic.util.PageList;
import cn.fayang.org.domain.Employee;
import cn.fayang.org.query.EmployeeQuery;
import cn.fayang.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 控制器
 *
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 通过ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.queryById(id);

    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping
    public List<Employee> qureyAll(){
        return employeeService.queryAll();
    }

    /**
     * 分页查询
     * @param employeeQuery
     * @return
     */
    @PostMapping
    public PageList<Employee> querPage(@RequestBody EmployeeQuery employeeQuery){
        return  employeeService.queryPage(employeeQuery);
    }

    /**
     * 删除
     * @param id
     * @return
     */

    @DeleteMapping("{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            employeeService.deleteByID(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后再试");
        }
    }
    @PatchMapping
    public AjaxResult Batchremove(@RequestBody List<Long> ids){
        try {
            employeeService.batchDelete(ids);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后再试");
        }
    }

    /**
     * 增加和修改
     * @param employee
     * @return
     */
    @PutMapping
    public  AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后再试");
        }
    }





}
